package gptma.bo.dao.querier;

/**
 * Represents a single criteria, that will be ANDED with the others (or not) in
 * the final query
 * 
 * @author xcl
 * 
 */
public class SearchCriterion {

	/**
	 * valid property to search for
	 */
	private String property;

	/**
	 * used searchMode
	 */
	private SearchMode mode;

	/**
	 * value to match the property
	 */
	private Object value;

	/**
	 * case sensitivity settings
	 */
	private boolean caseSensitive;

	/**
	 * Constructor, searchMode: equals, caseSensitive: true
	 * 
	 * @param property
	 *            property
	 * @param value
	 *            value
	 */
	public SearchCriterion(String property, Object value) {
		this(property, SearchMode.EQUALS, value);
	}

	public SearchCriterion(String property, Object value, boolean caseSensitive) {
		this(property, SearchMode.EQUALS, value, caseSensitive);
	}

	/**
	 * Constructor, caseSensitive: true
	 * 
	 * @param property
	 *            property
	 * @param mode
	 *            mode
	 * @param value
	 *            value
	 */
	public SearchCriterion(String property, SearchMode mode, Object value) {
		this(property, mode, value, true);
	}

	/**
	 * Constructor
	 * 
	 * @param property
	 *            property
	 * @param mode
	 *            mode
	 * @param value
	 *            value
	 * @param caseSensitive
	 *            caseSensitive
	 */
	public SearchCriterion(String property, SearchMode mode, Object value,
			boolean caseSensitive) {
		this.property = property;
		this.mode = mode;
		this.value = value;
		this.caseSensitive = caseSensitive;
	}

	/**
	 * @return the property
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @return the mode
	 */
	public SearchMode getMode() {
		return mode;
	}

	/**
	 * @return the value
	 */
	public Object getValue() {
		return value;
	}

	/**
	 * @return the caseSensitive
	 */
	public boolean isCaseSensitive() {
		return caseSensitive;
	}

	/**
	 * @param caseSensitive
	 *            the caseSensitive to set
	 */
	public void setCaseSensitive(boolean caseSensitive) {
		this.caseSensitive = caseSensitive;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((mode == null) ? 0 : mode.hashCode());
		result = prime * result
				+ ((property == null) ? 0 : property.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final SearchCriterion other = (SearchCriterion) obj;
		if (mode == null) {
			if (other.mode != null)
				return false;
		} else if (!mode.equals(other.mode))
			return false;
		if (property == null) {
			if (other.property != null)
				return false;
		} else if (!property.equals(other.property))
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}

	/**
	 * toString
	 * 
	 * @return a string representation of the object
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return new StringBuilder("SearchCriterion[property=").append(property)
				.append(", mode=").append(mode).append(", value=")
				.append(value).append("]").toString();
	}
}